package com.zhoug.widget.popup;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.ListPopupWindow;
import android.widget.PopupWindow;
import android.widget.TextView;


import com.zhoug.common.adapters.BaseListViewAdapter;
import com.zhoug.common.utils.LogUtils;
import com.zhoug.widget.R;

import java.lang.reflect.Field;
import java.util.List;

import androidx.annotation.NonNull;

public abstract class AutoCompletePopupList<T> extends ListPopupWindow {
    private static final String TAG = ">>>FirmListPopup";
    private List<T> data;
    private Context context;
    private Adapter adapter;
    private int selected;
    private Callback callback;
    private Drawable itemSelected;
    private boolean isInit = false;

    private String inputKey;

    public AutoCompletePopupList(@NonNull Context context) {
        super(context);
        this.context = context;
        setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
        setPromptPosition(ListPopupWindow.POSITION_PROMPT_BELOW);
//        setHeight(AppUtils.getScreenSize().y / 3);
    }

    private void init() {
        if (!isInit) {
            adapter = new Adapter();
            adapter.setSelected(selected);
            adapter.setContext(context);
            adapter.setData(data);
            setModal(true);
            setAdapter(adapter);
            if (itemSelected == null) {
                setListSelector(context.getResources().getDrawable(R.drawable.widgets_auto_complete_item_bg));
            } else {
                setListSelector(itemSelected);
            }
            setOnItemClickListener((parent, view, position, id) -> {
                dismiss();
                inputKey = getText(getItemData(position));
                if (callback != null) {
                    callback.OnItemSelected(position);
                }
                adapter.setSelected(selected = position);
            });
            isInit = true;
        }

    }

    private void setFocusable(boolean focusable) {
        try {
            Field mPopupField = ListPopupWindow.class.getDeclaredField("mPopup");
            mPopupField.setAccessible(true);
            PopupWindow mPopup = (PopupWindow) mPopupField.get(this);
            mPopup.setFocusable(focusable);
            mPopupField.setAccessible(false);
            LogUtils.d(TAG, "setFocusable:" + focusable);
        } catch (NoSuchFieldException | IllegalAccessException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void show() {
        init();
        super.show();
        setFocusable(false);
    }


    public void setCallback(Callback callback) {
        this.callback = callback;
    }

    /**
     * 输入款尖子改变
     * @param inputKey
     */
    public void setInputKey(String inputKey) {
        if (inputKey != null) {
            if (!inputKey.equals(this.inputKey)) {
                this.inputKey = inputKey;
                if(callback!=null){
                    callback.OnInputKeyChangeListener(inputKey);
                }
            }
        }


    }

    public List<T> getData() {
        return data;
    }

    public T getItemData(int position) {
        if (data != null && position >= 0 && position < data.size()) {
            return data.get(position);
        }
        return null;
    }

    public void setData(List<T> data) {
        this.data = data;
    }

    public void updateData(List<T> data) {
        if (data != null && data.size() > 0) {
            this.data.clear();
            this.data.addAll(data);
        } else {
            this.data.clear();
        }

    }

    public void notifyDataSetChanged() {
        if (this.adapter != null) {
            this.adapter.notifyDataSetChanged();
        }
    }

    public void setContext(Context context) {
        this.context = context;
    }


    public void setItemSelected(Drawable itemSelected) {
        this.itemSelected = itemSelected;
    }

    public void setSelected(int position) {
        this.selected = position;
    }

    public int getSelected() {
        if (adapter != null) {
            return selected = adapter.getSelected();
        }
        return 0;
    }

    public class Adapter extends BaseListViewAdapter<T> {
        private Context context;
        private int selected;

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View root = convertView;
            if (root == null) {
                root = LayoutInflater.from(context).inflate(R.layout.widgets_auto_complete_popu_list_item, null);
            }
            TextView tvName = root.findViewById(R.id.tv_name);
            T itemData = getItemData(position);
            if (itemData != null) {
                tvName.setText(getText(itemData));
            }
            return root;
        }


        private Context getContext() {
            return context;
        }

        private void setContext(Context context) {
            this.context = context;
        }

        private int getSelected() {
            return selected;
        }

        private void setSelected(int selected) {
            this.selected = selected;
        }


    }


    public abstract String getText(T t);


    public interface Callback {
        //选择回掉
        void OnItemSelected(int position);

        void OnInputKeyChangeListener(String inputKey);
    }


}
